package com.suishoupai.xmb.service;

import com.suishoupai.xmb.dao.CutSaleXiaoquMapper;
import com.suishoupai.xmb.dao.SaleXiaoquMapper;
import com.suishoupai.xmb.entity.SaleXiaoqu;
import com.suishoupai.xmb.entity.Xiaoqu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by caojian on 2017/4/9.
 */
@Service
public class SaleXiaoquService {
    @Autowired
    private SaleXiaoquMapper saleXiaoquMapper;
    @Autowired
    private CutSaleXiaoquMapper cutSaleXiaoquMapper;

    public List<Xiaoqu> getListBySaleId(long saleId) {
        return cutSaleXiaoquMapper.getListBySaleId(saleId);
    }

    /**
     * 该小区已在商家服务小区中
     *
     * @param xiaoquId
     * @param saleId
     * @return
     */
    public boolean hasSaleXiaoqu(long xiaoquId, long saleId) {
        return cutSaleXiaoquMapper.selectCountByPrimaryKey(xiaoquId, saleId) > 0;
    }

    public void add(SaleXiaoqu saleXiaoqu) {
        saleXiaoqu.setCreateTime(System.currentTimeMillis());
        if (saleXiaoquMapper.insertSelective(saleXiaoqu) == 0) {
            throw new RuntimeException("插入失败！");
        }
    }

    public void delete(long xiaoquId, long saleId) {
        if (cutSaleXiaoquMapper.deleteByPrimaryKey(xiaoquId, saleId) == 0) {
            throw new RuntimeException("删除失败！");
        }
    }
}
